package com.pig.provide.http.context;

import com.pig.common.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author by 李志伟
 * Create Date: 2019/1/8 11:34
 * Description:
 */
@Component
public class MethodSupport {
    @Autowired
    ApplicationContext applicationContext;

    public String invoke(RpcRequestContext context) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //找到对应的service
        Object bean = applicationContext.getBean(context.getClazz());
        //得到要调用的方法
        Method method = bean.getClass().getMethod(context.getMethodName(),context.getParameterTypes());
        //调用对应的方法
        Object invoke = method.invoke(bean, context.getParameter());
        return JsonUtils.objToJsonStr(invoke);
    }
}
